package com.itheima.health.controller;

import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.resources.MessageConstant;
import com.itheima.health.utils.resources.RedisMessageConstant;
import com.itheima.health.utils.sms.SmsUtils;
import com.itheima.health.utils.sms.ValidateCodeUtils;
import com.itheima.health.vo.SetmealVO;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
public class OrderController {

    @Reference
    private OrderService orderService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("order/sendCodeToPhone/{telephone}")
    public  Result  sendCodeToPhone(@PathVariable("telephone") String telephone){
        try{
           //  生成一个验证码  发送给客户手机
            String code = ValidateCodeUtils.generateValidateCode(4);
            System.out.println("校验的验证码："+code);
//            封装用户手机号和验证码
             //   消息 投递  mq
            Map  map = new HashMap();
            map.put("telephone",telephone);
            map.put("code",code);
//            rabbitTemplate.convertAndSend("heima-queue",map);
//            SmsUtils.validUserTelephone(telephone,code);
            redisTemplate.opsForValue().set(RedisMessageConstant.SENDTYPE_ORDER+telephone,code,90, TimeUnit.SECONDS);
            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        }catch (Exception e){
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }

    @GetMapping("order/findOrderDetailByOrderId/{id}")
    public  Result  findOrderDetailByOrderId(@PathVariable("id") int id){
        try{
              Map map = orderService.findOrderDetailByOrderId(id);
            return new Result(true, MessageConstant.QUERY_ORDER_DETAIL_SUCCESS,map);
        }catch (Exception e){
            return new Result(false, MessageConstant.QUERY_ORDER_DETAIL_FAIL);
        }
    }

    @PostMapping("order/add")
    public  Result  add(@RequestBody Map map){
        try{
            //  1. 判断表单提交的验证码和redis保存的验证码是否一致
            String codeForm  = (String)map.get("validateCode");
            String telephone  = (String)map.get("telephone");
            String redisCode = (String) redisTemplate.opsForValue().get(RedisMessageConstant.SENDTYPE_ORDER+telephone);
            if(redisCode==null){
                return new Result(false, "验证码过期！");
            }else{
                //  比对验证码
                if(redisCode.equals(codeForm)){
                      // 验证码正确
                    Order order =  orderService.add(map); //  预约失败 。。。add方法 抛出异常 给出指定信息
                    return new Result(true, MessageConstant.ADD_ORDER_SUCCESS,order);
                }else{
                    //  验证码输入错误
                    return new Result(false, "验证码输入错误");
                }
            }
        }catch (Exception e){
            return new Result(false, e.getMessage());
        }
    }


}
